package com.masterclient.app.modules.order.adapter;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.masterclient.app.R;
import com.masterclient.app.base.MainViewHolder;
import com.eoner.baselibrary.bean.goods.CouponMessage;
import com.eoner.baselibrary.bean.order.ConfirmOrderCoupons;

public class SubmitOrderCouponAdapter extends DelegateAdapter.Adapter<MainViewHolder> {

    Activity mContext;
    private LayoutHelper mLayoutHelper;
    private VirtualLayoutManager.LayoutParams mLayoutParams;
    private int mCount = 0;
    ConfirmOrderCoupons data;
    private String salesRuleId;
    private String salesRuleName;
    private String isDeductCommission = "1";
    private String couponCommissionTxt;
    private String showYhJm;
    OnCouponClick onCouponClick;

    private RelativeLayout rlYhq;
    private TextView tvYhq;
    private TextView tvYhqNum;
    private TextView use_coupon_flag;
    private ImageView ivYhqArrow;
    private boolean isGiving;

    public interface OnCouponClick {
        void OnItemClick(int position);
    }

    public void setOnCouponClick(OnCouponClick onCouponClick) {
        this.onCouponClick = onCouponClick;
    }

    public SubmitOrderCouponAdapter(Activity context, LayoutHelper layoutHelper, int count,boolean isGiving) {
        this(context, layoutHelper, count, null, null,isGiving);
    }

    public SubmitOrderCouponAdapter(Activity context, LayoutHelper layoutHelper, int count, @NonNull VirtualLayoutManager.LayoutParams layoutParams, ConfirmOrderCoupons data,boolean isGiving) {
        this.mContext = context;
        this.mLayoutHelper = layoutHelper;
        this.mCount = count;
        this.isGiving = isGiving;
        this.mLayoutParams = layoutParams;
        this.data = data;
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mLayoutHelper;
    }

    @Override
    protected void onBindViewHolderWithOffset(MainViewHolder holder, int position, int offsetTotal) {

    }

    @Override
    public MainViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return null;
    }

    @Override
    public void onBindViewHolder(MainViewHolder holder, int position) {
        if (mLayoutParams != null) {
            holder.itemView.setLayoutParams(
                    new VirtualLayoutManager.LayoutParams(mLayoutParams));
        }

        rlYhq = holder.itemView.findViewById(R.id.rl_yhq);
        use_coupon_flag = holder.itemView.findViewById(R.id.use_coupon_flag);

        tvYhq = holder.itemView.findViewById(R.id.tv_yhq);
        tvYhqNum = holder.itemView.findViewById(R.id.tv_yhq_num);
        ivYhqArrow = holder.itemView.findViewById(R.id.iv_yhq_arrow);

        TextView zsqTextView = holder.itemView.findViewById(R.id.zsq_coupon_flag);
        if (isGiving) {
            zsqTextView.setVisibility(View.VISIBLE);
            rlYhq.setVisibility(View.GONE);
            use_coupon_flag.setVisibility(View.GONE);

        } else {
            rlYhq.setVisibility(View.VISIBLE);
            zsqTextView.setVisibility(View.GONE);
            //切换地址时不修改优惠券状态
            if (data != null && data.getValids() != null
                    && data.getValids().size() > 0) {

                ivYhqArrow.setVisibility(View.VISIBLE);
                rlYhq.setClickable(true);
                rlYhq.setOnClickListener(v -> {
                    onCouponClick.OnItemClick(0);
                });
                //默认选择优惠券
                for (CouponMessage couponMessage : data.getValids()) {
                    if (couponMessage.getSelect_status()) {
                        salesRuleId = couponMessage.getCouponId();
                        salesRuleName = couponMessage.getName();
                        isDeductCommission = couponMessage.getIsDeductCommission();
                        couponCommissionTxt = couponMessage.getCouponCommissionText();
                    }
                }

                //有选中优惠券
                if (!TextUtils.isEmpty(salesRuleId)) {
                    tvYhqNum.setText("已选1张优惠券");
                    tvYhqNum.setVisibility(View.VISIBLE);
                    if (!TextUtils.isEmpty(couponCommissionTxt)) {
                        use_coupon_flag.setVisibility(View.VISIBLE);
                        use_coupon_flag.setText(couponCommissionTxt);
                    } else {
                        use_coupon_flag.setVisibility(View.GONE);
                    }

                    for (CouponMessage couponMessage : data.getValids()) {
                        if (salesRuleId.equals(couponMessage.getCouponId())) {
                            showYhJm = couponMessage.getCoupon_discount_total();
                        }
                    }
                    tvYhq.setText("-¥" + showYhJm);
                } else {
                    //无选中优惠券
                    tvYhqNum.setVisibility(View.GONE);
                    use_coupon_flag.setVisibility(View.GONE);
                    tvYhq.setText(data.getValids().size() + "张可用");
                }
            } else {
                tvYhqNum.setVisibility(View.GONE);
                use_coupon_flag.setVisibility(View.GONE);
                tvYhq.setText("您当前没有可用优惠券");
                tvYhq.setTextColor(0xffcccccc);
                ivYhqArrow.setVisibility(View.INVISIBLE);
                rlYhq.setOnClickListener(v -> {
                    if (isGiving) {
                        return;
                    }
                    onCouponClick.OnItemClick(1);
                });
            }
        }
    }

    public void refreshUI(ConfirmOrderCoupons confirmOrderCoupons) {
        showYhJm = "0";
        salesRuleId = "";
        salesRuleName = "";
        this.data = confirmOrderCoupons;
        notifyDataSetChanged();
    }


    @Override
    public int getItemCount() {
        return mCount;
    }

    @Override
    public int getItemViewType(int position) {
        return 8;
    }
}
